<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a title="全部" href="#">全部</a>
                </li>
                <li v-for="(oneSubject,index) in oneSubjectList" :key="oneSubject.id" :class="{active:oneIndex === index}">
                  <a :title="oneSubject.title" @click="searchOne(oneSubject.id,index)" href="#">{{ oneSubject.title }}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li v-for="(twoSubject,index) in twoSubjectList" :key="twoSubject.id" :class="{active:twoIndex === index}">
                  <a :title="twoSubject.title" @click="searchTwo(twoSubject.id,index)" href="#">{{ twoSubject.title }}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li :class="{'current bg-orange':buyCountSort !== ''}">
                <a title="关注度" href="#" @click="searchBuyCount()">关注度
                  <span :class="{hide:buyCountSort===''}">↓</span></a>
              </li>
              <li :class="{'current bg-orange':gmtCreateSort!== ''}">
                <a title="最新" href="#" @click="searchGmtCreate()">最新
                  <span :class="{hide:gmtCreateSort===''}">↓</span></a>
              </li>
              <li :class="{'current bg-orange':priceSort!==''}">
                <a title="价格" href="#" @click="searchPrice()">价格&nbsp;
                  <span :class="{hide:priceSort===''}">↓</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="data.total === 0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，小编正在努力整理中...</span>
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list">
            <ul class="of" id="bna">
              <li v-for="item in data.items" :key="item.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :src="item.cover" class="img-responsive" :alt="item.title">
                    <div class="cc-mask">
                      <a :href="`/course/${item.id}`" :title="item.title" class="comm-btn c-btn-1">{{ item.title }}</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a :href="`/course/${item.id}`" :title="item.title" class="course-title fsize18 c-333">{{ item.title }}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green" v-if="Number(item.price === 0)">
                      <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{item.viewCount}}人学习</i>
                      |
                      <i class="c-999 f-fA">9634评论</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <div>
          <el-pagination
            @current-change="getCourseByCondition"
            :current-page="current"
            :page-size="pageSize"
            layout="total, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
import courseApi from '../../api/course'
export default {
  data(){
    return{
      oneSubjectList:[], // 一级分类（包含二级分类）
      twoSubjectList:[], // 二级分类
      data:{} ,// 课程列表里面包含分页相关信息（也可以抽取出来）
      current:1, //当前页
      pageSize: 4, // 页大小
      total: 0 ,// 总记录数
      conditionObj: {}, // 查询表单对象
      oneIndex: -1, // 点击一级分类(为了让点击样式生效)
      twoIndex: -1, // 点击二级分类(为了让点击样式生效)
      buyCountSort:'', // (为了让点击样式生效)
      gmtCreateSort:'', // 为了让点击样式生效\
      priceSort:'',// 为了让点击样式生效
    }
  },
  created() {
    this.getAllSubject();
    this.getCourseByCondition();
  },
  methods:{
    // 查询一级分类
    getAllSubject(){
      courseApi.getAllSubject().then(res=>{
        this.oneSubjectList = res.data.data.list;
      })
    },
    // 通过一级分类查询二级分类
    searchOne(oneSubjectId,index){
      // 循环每一个一级分类id找到和点击事件相等的id
      for(let i=0;i<this.oneSubjectList.length;i++){
        let oneSubject = this.oneSubjectList[i];
        if(oneSubjectId === oneSubject.id){
          this.twoSubjectList = oneSubject.children;
        }
      }
      // 把index赋值给oneindex，激活样式
      this.oneIndex = index;
      // 再次点击一级分类时清空二级分类
      this.twoIndex = -1;
      this.conditionObj.subjectId = ''; //

      // 装配条件
      this.conditionObj.subjectParentId = oneSubjectId;
      // 刷新页面
      this.getCourseByCondition();

    },
    // 点击二级分类，装配查询条件
    searchTwo(twoSubjectId,index){
      this.twoIndex = index;
      // 装配条件
      this.conditionObj.subjectId = twoSubjectId;
      // 刷新页面
      this.getCourseByCondition();
    },
    // 排序
    searchBuyCount(){
      // 设置变量值为了让点击样式生效
      this.buyCountSort = '1';
      this.gmtCreateSort = '';
      this.priceSort = '';
      // 装配条件
      this.conditionObj.buyCountSort = this.buyCountSort;
      this.conditionObj.gmtCreateSort = this.gmtCreateSort;
      this.conditionObj.priceSort = this.priceSort;
      // 刷新页面
      this.getCourseByCondition();
    },
    searchGmtCreate(){
      // 设置变量值为了让点击样式生效
      this.buyCountSort = '';
      this.gmtCreateSort = '1';
      this.priceSort = '';
      // 装配条件
      this.conditionObj.buyCountSort = this.buyCountSort;
      this.conditionObj.gmtCreateSort = this.gmtCreateSort;
      this.conditionObj.priceSort = this.priceSort;
      // 刷新页面
      this.getCourseByCondition();
    },
    searchPrice(){
      // 设置变量值为了让点击样式生效
      this.buyCountSort = '';
      this.gmtCreateSort = '';
      this.priceSort = '1';
      // 装配条件
      this.conditionObj.buyCountSort = this.buyCountSort;
      this.conditionObj.gmtCreateSort = this.gmtCreateSort;
      this.conditionObj.priceSort = this.priceSort;
      // 刷新页面
      this.getCourseByCondition();
    },
    // 课程列表
    getCourseByCondition(current = 1){
      this.current = current
      courseApi.getCourseByCondition(this.current,this.pageSize,this.conditionObj).then(res=>{
        this.data = res.data.data.map;
        this.current = res.data.data.map.current;
        this.pageSize = res.data.data.map.pageSize;
        this.total = res.data.data.map.total;
      })
    }
  }
};
</script>

<style scoped>
.active {
  background: #bdbdbd;
}
.hide {
  display: none;
}
.show {
  display: block;
}
</style>
